package com.my.users.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.my.common.util.PageUtil;
import com.my.users.dao.IAUserDao;
import com.my.users.pojo.AUser;
import com.my.users.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService
{
	@Resource
	private IAUserDao userDao;

	@Override
	public AUser findOneAUserService(Map<String, Object> condMap)
	{
		return this.userDao.findOne(condMap);
	}

	@Override
	public List<AUser> findListAUserService(PageUtil page, Map<String, Object> condMap)
	{
		List<AUser> usersList = new ArrayList<AUser>();
		/*
		 * 处理一下关键字
		 * keyword 变成: % + keyword + %
		 * */
		if(condMap.get("keyword") != null)
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		
		/*pageCount == true:拿总记录数:
		pageCount == false:分页数据:
		pageCount 为null不分页查询*/
		if(page != null)
		{
			//分页查询
			/* 先查询总记录数 */
			condMap.put("pageCount", "true");
			usersList = this.userDao.findList(condMap);
			/* adminsList有一条记录,总记录数放到了id属性上 */
			page.setTotalRecord(usersList.get(0).getId());
			
			/* 拿分页数据 */
			condMap.put("pageCount", "false");
			/* 当前记录数
			 * 每页多少条 */
			condMap.put("currentRecord", page.getCurrRecord());
			condMap.put("pageSize", page.getPageSize());
			usersList = this.userDao.findList(condMap);
		}else
		{
			//不分页查询
			usersList = this.userDao.findList(condMap);
		}
		return usersList;
	}

	@Override
	public JSONObject insertAUserService(AUser user)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.userDao.insert(user);

		/*
		 * String str = null ; str.toString() ;
		 */

		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "添加成功");

			/* 二级结构 */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", user.getId());
			resultJSON.put("data", dataJSON);
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "添加失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateAUserService(AUser user)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.userDao.update(user);

		/*
		 * String str = null ; str.toString() ;
		 */

		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "更新成功");
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "更新失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteAUserService(Map<String, Object> condMap)
	{
		JSONObject resultJSON = new JSONObject();
		int res = this.userDao.delete(condMap);

		/*
		 * String str = null ; str.toString() ;
		 */

		if (res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "删除成功");
		} else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", "删除失败");
		}
		return resultJSON;
	}

}
